﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
    public static class DanhmucDAL
    {
        public static DanhmucObj GetByID(string DanhmucID)
        {
            DanhmucObj obj = new DanhmucObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDanhmuc_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@DanhmucID", DanhmucID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<DanhmucObj> GetByAll()
        {
            List<DanhmucObj> result = new List<DanhmucObj>();
            DanhmucObj obj = new DanhmucObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDanhmuc_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static int Insert(DanhmucObj obj)
        {
            int value = 0;
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDanhmuc_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Tendanhmuc", (obj.Tendanhmuc == null) ? String.Empty : obj.Tendanhmuc);
                    cmd.Parameters.AddWithValue("@Thutu", (obj.Thutu == null) ? "0" : obj.Thutu);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    cmd.Parameters.AddWithValue("@Title", (obj.Title == null) ? String.Empty : obj.Title);
                    cmd.Parameters.AddWithValue("@Keyword", (obj.Keyword == null) ? String.Empty : obj.Keyword);
                    cmd.Parameters.AddWithValue("@Description", (obj.Description == null) ? String.Empty : obj.Description);

                    SqlParameter id = new SqlParameter("@ID", DbType.Int32);
                    id.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(id);

                    Database.db.ExecuteNonQuery(cmd);

                    value = Int32.Parse(cmd.Parameters["@ID"].Value.ToString());
                }
            }
            catch (Exception)
            {

            }
            return value;
        }

        public static bool Update(DanhmucObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDanhmuc_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DanhmucID", (obj.DanhmucID == null) ? String.Empty : obj.DanhmucID);
                    cmd.Parameters.AddWithValue("@Tendanhmuc", (obj.Tendanhmuc == null) ? String.Empty : obj.Tendanhmuc);
                    cmd.Parameters.AddWithValue("@Thutu", (obj.Thutu == null) ? "0" : obj.Thutu);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    cmd.Parameters.AddWithValue("@Title", (obj.Title == null) ? String.Empty : obj.Title);
                    cmd.Parameters.AddWithValue("@Keyword", (obj.Keyword == null) ? String.Empty : obj.Keyword);
                    cmd.Parameters.AddWithValue("@Description", (obj.Description == null) ? String.Empty : obj.Description);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string DanhmucID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbDanhmuc_DeleteByID";
                    cmd.Parameters.AddWithValue("@DanhmucID", DanhmucID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
